#!/bin/sh
PREREQ=""

prereqs()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

# To avoid fsck
touch ${rootmnt}/fastboot
:> ${rootmnt}/etc/fstab

cat <<EOF > ${rootmnt}/etc/rc2.d/S05inici
#!/bin/sh
#
# ===========================================================
# system functions
# ===========================================================

# Mount device \$1 to \$2
# \$1 = /dev device to mount, eg. /dev/hda1
# \$2 = mountpoint, eg. /mnt/hda1
# \$3 = mount options, for example "loop", "ro", or "remount,rw"
#

mount_device()
{
  mkdir -p \$2
  if [ "\$3" != "" ]; then OPTIONS="-o \$3"; else OPTIONS=""; fi

  mount -t auto \$1 \$2 \$OPTIONS >> /var/log/initramfs.debug 2>&1
  err=\$?

  if [ "\$err" -ne 0 ]; then rmdir \$2 >> /var/log/initramfs.debug 2>&1; fi

  return \$err
}

# ===========================================================
# discovery functions
# ===========================================================

# List all CD-ROMs
# by using /proc entries/etc/initramfs-tools-live/scripts/live-bottom/inicialitza
#
list_cdrom_devices()
{
   for CDDEVICE in `cat /proc/sys/dev/cdrom/info | head -n 3 | tail -n 1 | cut -d ":" -f 2`; do
      echo "/dev/\$CDDEVICE"
   done
}

# List all partition devices
# take list of all partitions and output unique disks.
# Return empty result when nohd parameter was given.
#
list_partition_devices()
{
#   cat /proc/partitions | grep -v loop | sed -r "s/^[0-9[:space:]]+/\/dev\//" | grep /dev/
#    fdisk -ul | egrep "Linux" | egrep -v "La" | egrep -v "swap" | egrep -v "Par" | cut -d " " -f 1
     fdisk -ul | egrep  "^/" | egrep -v " 82 " | egrep -v " [f5] " | cut -d " " -f 1
}

# List all disk devices
#
list_disk_devices()
{
   list_partition_devices | egrep -v "[0-9]"
}

# List all block devices
#
list_block_devices()
{
   list_cdrom_devices
   list_partition_devices
}

# Try to mount all disks, partitions and cdroms and Find where the LiveCD is.
# If LiveCD found in the device, echo dirname of it's directory,
# and leave the device mounted. Mounting is not ro, but without any argument.
# \$1 = directory where devices will be mounted
#
find_live_data_dir()
{
   list_block_devices | while read DEVICE; do
      DIR="/media/\$(basename \$DEVICE)"
      mount_device \$DEVICE \$DIR
   done
}

# create correct fstab file in \$1/etc/fstab and create apropriate
# mount directories in \$1
# \$1 = root directory (union)
#
activate_fstab()
{
   FSTAB="/etc/fstab"
   echo "proc             /proc            proc        defaults         0   0" >\$FSTAB

   list_cdrom_devices | while read DEVICE; do
      MOUNTDIR="/media/\$(basename \$DEVICE)"
      mkdir -p \$MOUNTDIR
      echo "\$DEVICE \$MOUNTDIR udf,iso9660  ro,user,noauto 0 0" >>\$FSTAB
   done
   list_partition_devices | while read DEVICE; do
      MOUNTDIR="/media/\$(basename \$DEVICE)"
      mount_device \$DEVICE \$MOUNTDIR
      umount \$MOUNTDIR 2>/dev/null
      if [ \$? -eq 0 ]; then # add to fstab only if mount+umount is OK
         echo "\$DEVICE \$MOUNTDIR auto auto,users,suid,dev,exec 0 0" >>\$FSTAB
      else # remove empty directory
         rmdir \$MOUNTDIR 2>/dev/null
      fi
   done

   fdisk -l 2>/dev/null | grep -i "Linux swap" | egrep "^/dev/" \
     | cut -f 1 -d " " | sed -r "s/(.+)/\\1  none	swap	sw	0 0/" >>\$FSTAB
}

rm -f /etc/mtab*
cat /proc/mounts > /etc/mtab
activate_fstab
mount -a  >> /var/log/initramfs.debug 2>&1
swapon -a >> /var/log/initramfs.debug 2>&1

list_vfat()
{
mount | grep vfat | cut -d " " -f 1
}

list_ntfs()
{
mount | grep ntfs | cut -d " " -f 1
}

FUSE="/usr/bin/ntfs-3g"

list_ntfs | while read NTFS; do
NTFSS=\$(basename \$NTFS)
sed -e '/'"\$NTFSS "'/d' < /etc/fstab > /etc/fstab.tmp
cp /etc/fstab.tmp /etc/fstab
MOUNTDIR="/media/\$NTFSS"
if [ \$NTFS != "" ]; then
   umount \$NTFS
if [ -f \$FUSE ]; then
   mount -t ntfs-3g -o silent,umask=0,locale=ca_ES.utf8 \$NTFS \$MOUNTDIR
   echo "\$NTFS \$MOUNTDIR ntfs-3g silent,umask=0,locale=ca_ES.utf8 0 0" >> /etc/fstab
else
   mount -t ntfs -o ro,gid=users,umask=0222 \$NTFS \$MOUNTDIR
   echo "\$NTFS \$MOUNTDIR ntfs ro,auto,gid=users,umask=0222 0 0" >> /etc/fstab
fi
fi
done


list_vfat | while read VFAT; do
VFATT=\$(basename \$VFAT)
sed -e '/'"\$VFATT "'/d' < /etc/fstab > /etc/fstab.tmp
cp /etc/fstab.tmp /etc/fstab
MOUNTDIR="/media/\$VFATT"
if [ \$VFAT != "" ]; then
   umount \$VFAT
   mount -t vfat -o auto,users,umask=0000 \$VFAT \$MOUNTDIR
   echo "\$VFAT \$MOUNTDIR vfat auto,users,umask=0000 0 0" >> /etc/fstab
fi
done

get_device_vfat ()
{
	dev=\$(grep /.dirs/dev /proc/mounts | cut -d " " -f 1 | cut -d "/" -f 3)
	echo \$dev
}
get_device_ext2 ()
{
	dev=\$(grep /.dirs/changes /proc/mounts | grep ext2 | cut -d " " -f 1 | cut -d "/" -f 3)
	echo \$dev
}
get_dirconf ()
{
	dev=\$(cat /proc/mounts | egrep "/ " | egrep "=rw" | cut -d "/" -f 5 | cut -d "=" -f 1)
	echo \$dev
}

#Tema configuracions
device_vfat=\`get_device_vfat\`
device_ext2=\`get_device_ext2\`
dir_dirconf=\`get_dirconf\`

#Tema Xorg ... amb sort ...
if [ -f /media/\$device_ext2/\$dir_dirconf/.configurat ]; then
break
else
:> /etc/X11/xorg.conf

#dexconf
#readlink /etc/X11/X | md5sum > /var/lib/x11/X.md5sum
#md5sum /etc/X11/xorg.conf > /var/lib/x11/xorg.conf.md5sum
#md5sum /etc/X11/Xwrapper.config > /var/lib/x11/Xwrapper.config.md5sum
#/var/lib/dpkg/info/xserver-xorg.postinst
dpkg-reconfigure -fnoninteractive -phigh xserver-xorg &> /dev/null
#Problemes amb la resolució del driver nv ?
cat /etc/X11/xorg.conf | egrep "nv"
if [ \$? = "0" ]; then
echo "En alguns casos el manejador \"nv\" no s'autoconfigura correctament."
read -p "Vols emprar el manejador \"vesa\"?(S/n)" resposta
if [ "\$resposta" != "n" ]; then
cat  /etc/X11/xorg.conf | sed -e "s/\"nv\"/\"vesa\"/g" > /etc/X11/xorg.conf.new
mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
echo "Emprant el manejador vesa, sort ..."
sleep 1
fi
fi
if [ "${dirconf}" != ""  ]; then

#Tema Configuracions Persistents

touch /media/\$device_ext2/\$dir_dirconf/.configurat

dirs=\`ls /media/\$device_ext2 | egrep -v "^lost"\`

rm -f /media/\$device_vfat/configs.txt

echo "Multiples Configuracions Persistents" > /media/\$device_vfat/configs.txt
echo " " >> /media/\$device_vfat/configs.txt

for dir in \$dirs; do
echo "\$dir" >> /media/\$device_vfat/configs.txt
done
sync

fi
fi
rm -f /var/log/initramfs.debug
rm -f /etc/rc2.d/S05inici
EOF

${rootmnt}/bin/chmod +x ${rootmnt}/etc/rc2.d/S05inici
